//
//  ViewController.swift
//  InkeLiveMac
//
//  Created by Mac on 2021/7/27.
//

import Cocoa
import SnapKit
import KSPlayer

class ViewController: NSViewController {
    
    var model = InkeResponseModel()
    
    private var window = MainWindow()

    override func viewDidLoad() {
        super.viewDidLoad()
        title = "映客直播"
        // Do any additional setup after loading the view.
        requestData()
        view.addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    func requestData(){
        FDNetwork.GET(url: "http://service.inke.com/api/live/near_recommend", param: nil) { result in
            self.model = InkeResponseModel.deserialize(from: result) ?? InkeResponseModel()
            self.collectionView.reloadData()
        } failure: { error in
            debugPrint(error)
        }

    }
    
    lazy var collectionView: NSCollectionView = {
        let layout = NSCollectionViewFlowLayout()
        let collectionView = NSCollectionView(frame: .zero)
        collectionView.collectionViewLayout = layout
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.isSelectable = true
        collectionView.register(LiveItem.classForCoder(), forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "LiveItem"))
        return collectionView
    }()

    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }


}

extension ViewController: NSCollectionViewDelegate,NSCollectionViewDataSource,NSCollectionViewDelegateFlowLayout{
    func numberOfSections(in collectionView: NSCollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
        return model.data.count
    }
    
    func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
        let cell = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "LiveItem"), for: indexPath) as! LiveItem
        if indexPath.item < model.data.count {
            cell.model = model.data[indexPath.item]
        }
        return cell
    }
    
    func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
        let width = 200
        return NSSize(width: width, height: width)
    }
    
    func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 5
    }
    
    func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 5
    }
    
    func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets {
        return NSEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
    }
    
    func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
        let data = model.data[indexPaths.first?.item ?? 0]
        window.open(url: URL(string: data.live_info.stream_addr)!)
    }
}

